<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>居民信息管理系统 - 居民列表</title>
  <!-- 引入Bootstrap 5 -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
  <style>
    :root {
      --primary-color: #4361ee;
      --secondary-color: #3a0ca3;
      --light-bg: #f8f9fa;
    }

    body {
      background-color: #f5f7fa;
      padding: 20px;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }

    .header-section {
      background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
      color: white;
      padding: 25px 30px;
      border-radius: 12px;
      margin-bottom: 25px;
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
    }

    .search-form {
      background: white;
      padding: 20px;
      border-radius: 12px;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
      margin-bottom: 25px;
    }

    .table-container {
      background: white;
      border-radius: 12px;
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
      overflow: hidden;
      margin-bottom: 25px;
    }

    .table-header {
      background-color: #f8f9fa;
      font-weight: 600;
    }

    .table > :not(:first-child) {
      border-top: none;
    }

    .action-btn {
      padding: 5px 10px;
      margin: 0 3px;
      font-size: 0.85rem;
    }

    .btn-add {
      background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
      color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 8px;
      font-weight: 500;
      transition: all 0.3s ease;
      display: inline-flex;
      align-items: center;
      gap: 8px;
    }

    .btn-add:hover {
      transform: translateY(-2px);
      box-shadow: 0 7px 14px rgba(67, 97, 238, 0.3);
    }

    .error-message {
      background: #ffebee;
      color: #c62828;
      padding: 15px;
      border-radius: 8px;
      margin: 20px 0;
      display: flex;
      align-items: center;
      gap: 12px;
    }

    .status-badge {
      padding: 5px 12px;
      border-radius: 20px;
      font-size: 0.85rem;
      font-weight: 500;
    }

    .status-healthy {
      background-color: #e8f5e9;
      color: #2e7d32;
    }

    .status-warning {
      background-color: #fff8e1;
      color: #f57f17;
    }

    .status-critical {
      background-color: #ffebee;
      color: #c62828;
    }

    .table-hover tbody tr:hover {
      background-color: rgba(67, 97, 238, 0.05);
    }
  </style>
</head>
<body>
<div class="container">
  <div class="header-section">
    <h1><i class="bi bi-people-fill"></i> 居民信息管理系统</h1>
    <p class="mb-0">居民信息管理与维护</p>
  </div>

  <div class="search-form">
    <form action="resident" method="get" class="row g-3 align-items-center">
      <input type="hidden" name="action" value="search">
      <div class="col-md-8">
        <div class="input-group">
          <span class="input-group-text"><i class="bi bi-search"></i></span>
          <input type="text" id="keyword" name="keyword" class="form-control form-control-lg" placeholder="输入姓名、地址或健康状态进行搜索...">
        </div>
      </div>
      <div class="col-md-4">
        <button type="submit" class="btn btn-primary w-100 btn-lg">
          <i class="bi bi-search me-2"></i>搜索居民
        </button>
      </div>
    </form>
  </div>

  <% if (request.getAttribute("error") != null) { %>
  <div class="error-message">
    <i class="bi bi-exclamation-circle"></i>
    <div><%= request.getAttribute("error") %></div>
  </div>
  <% } %>

  <div class="table-container">
    <div class="table-responsive">
      <table class="table table-hover align-middle">
        <thead class="table-header">
        <tr>
          <th scope="col">ID</th>
          <th scope="col">姓名</th>
          <th scope="col">性别</th>
          <th scope="col">出生日期</th>
          <th scope="col">地址</th>
          <th scope="col">健康状态</th>
          <th scope="col" class="text-center">操作</th>
        </tr>
        </thead>
        <tbody>
        <%
          java.util.List<com.example.bean.Resident> residentList = (java.util.List<com.example.bean.Resident>) request.getAttribute("residentList");
          if (residentList != null && !residentList.isEmpty()) {
            for (com.example.bean.Resident resident : residentList) {
              String healthStatus = resident.getHealthStatus() != null ? resident.getHealthStatus().toLowerCase() : "";
              String statusClass = "status-healthy";
              if (healthStatus.contains("异常") || healthStatus.contains("警告")) {
                statusClass = "status-warning";
              } else if (healthStatus.contains("严重") || healthStatus.contains("危险")) {
                statusClass = "status-critical";
              }
        %>
        <tr>
          <td><span class="badge bg-light text-dark"><%= resident.getId() %></span></td>
          <td><strong><%= resident.getName() %></strong></td>
          <td><%= resident.getGender() %></td>
          <td><%= new java.text.SimpleDateFormat("yyyy-MM-dd").format(resident.getBirthDate()) %></td>
          <td><%= resident.getAddress() %></td>
          <td>
              <span class="status-badge <%= statusClass %>">
                <i class="bi bi-heart-pulse me-1"></i><%= resident.getHealthStatus() %>
              </span>
          </td>
          <td class="text-center">
            <a href="resident?action=delete&id=<%= resident.getId() %>"
               class="btn btn-sm btn-danger action-btn"
               onclick="return confirm('确定要删除该居民信息吗？此操作不可恢复！')">
              <i class="bi bi-trash"></i> 删除
            </a>
            <a href="javascript:void(0);"
               class="btn btn-sm btn-primary action-btn"
               onclick="editResident(<%= resident.getId() %>)">
              <i class="bi bi-pencil"></i> 修改
            </a>
          </td>
        </tr>
        <%
          }
        } else {
        %>
        <tr>
          <td colspan="7" class="text-center py-5 text-muted">
            <i class="bi bi-info-circle" style="font-size: 2rem;"></i>
            <p class="mt-3">未找到居民信息</p>
            <a href="residentRegistration.jsp" class="btn btn-sm btn-outline-primary mt-2">
              <i class="bi bi-plus-circle"></i> 添加新居民
            </a>
          </td>
        </tr>
        <% } %>
        </tbody>
      </table>
    </div>
  </div>

  <div class="d-flex justify-content-between align-items-center">
    <div>
      <a href="residentRegistration.jsp" class="btn-add">
        <i class="bi bi-person-plus"></i> 新增居民信息
      </a>
    </div>
    <div class="text-muted">
      <small>共找到 <strong><%= residentList != null ? residentList.size() : 0 %></strong> 条记录</small>
    </div>
  </div>
</div>

<script>
  function editResident(id) {
    window.location.href = "residentEdit.jsp?id=" + id;
  }
</script>

<!-- 引入Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>